import React from 'react';
import { Alert, Result, Button, ResultProps } from 'antd';
import { useNavigate } from 'react-router-dom';
import useSimpleAuth from '../../hooks/useSimpleAuth';

/**
 * 部门隔离错误类型枚举
 */
export enum DepartmentErrorType {
  DEPARTMENT_ISOLATION = 'department_isolation',
  ROLE_RESTRICTION = 'role_restriction',
  NO_PERMISSION = 'no_permission'
}

/**
 * 部门隔离错误处理组件接口定义
 */
interface DepartmentIsolationErrorProps {
  /** 错误类型 */
  errorType: DepartmentErrorType | string;
  /** 资源类型名称 */
  resourceType?: string;
  /** 目标部门 */
  department?: string;
  /** 显示返回按钮 */
  showBackButton?: boolean;
  /** 自定义样式 */
  style?: React.CSSProperties;
  /** 显示为简单模式（警告条）而非完整结果页 */
  simple?: boolean;
}

/**
 * 部门隔离错误处理组件
 * 用于统一处理部门隔离相关的错误提示
 */
export const DepartmentIsolationErrorHandler: React.FC<DepartmentIsolationErrorProps> = ({ 
  errorType, 
  resourceType = '资源',
  department,
  showBackButton = true,
  style,
  simple = false
}) => {
  const navigate = useNavigate();
  const { getUserRegion } = useSimpleAuth();
  const userRegion = getUserRegion();
  
  // 如果是简单模式，返回警告条
  if (simple) {
    if (errorType === DepartmentErrorType.DEPARTMENT_ISOLATION) {
      return (
        <Alert
          message={`部门隔离：您无权访问 ${department || '其他'} 部门的${resourceType}`}
          description={`您只能操作 ${userRegion || '您自己'} 部门的资源`}
          type="error"
          showIcon
          style={style}
        />
      );
    } else if (errorType === DepartmentErrorType.ROLE_RESTRICTION) {
      return (
        <Alert
          message={`角色限制：您的角色无权执行此操作`}
          description="请联系管理员获取相应权限"
          type="error"
          showIcon
          style={style}
        />
      );
    } else {
      return (
        <Alert
          message={`权限不足`}
          description="您没有足够的权限执行此操作"
          type="error"
          showIcon
          style={style}
        />
      );
    }
  }
  
  // 根据错误类型生成不同的提示内容
  const getErrorConfig = () => {
    switch (errorType) {
      case DepartmentErrorType.DEPARTMENT_ISOLATION:
        return {
          status: '403' as ResultProps['status'],
          title: '部门隔离',
          subTitle: `您无权访问${department ? ` ${department} ` : '其他'}部门的${resourceType}`,
          extra: showBackButton && <Button type="primary" onClick={() => navigate(-1)}>返回</Button>,
          description: userRegion ? `您只能操作 ${userRegion} 部门的资源` : undefined
        };
      case DepartmentErrorType.ROLE_RESTRICTION:
        return {
          status: '403' as ResultProps['status'],
          title: '角色限制',
          subTitle: `您的角色无权执行此操作`,
          extra: showBackButton && <Button type="primary" onClick={() => navigate('/')}>返回首页</Button>
        };
      default:
        return {
          status: '403' as ResultProps['status'],
          title: '权限不足',
          subTitle: `您没有足够的权限执行此操作`,
          extra: showBackButton && <Button type="primary" onClick={() => navigate('/')}>返回首页</Button>
        };
    }
  };
  
  const config = getErrorConfig();
  
  return (
    <Result
      status={config.status}
      title={config.title}
      subTitle={config.subTitle}
      extra={config.extra}
      style={style}
    />
  );
};

export default DepartmentIsolationErrorHandler; 